package com.idbk.chargestation.activity.user;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.idbk.chargestation.R;
import com.idbk.chargestation.adapter.MyCommentAdapter;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonBase;
import com.idbk.chargestation.bean.JsonComment;
import com.idbk.chargestation.bean.JsonMyCommentList;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.Const;
import com.idbk.chargestation.util.GsonUtils;
import com.idbk.chargestation.util.UIUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的评论
 * @author liangzw,lupc
 *
 */
public class ActivityMyComment extends BaseActivity implements OnItemLongClickListener,OnClickListener{

	public final static String TAG = ActivityMyComment.class.getSimpleName();

	private Context mContext;
	private PullToRefreshListView mListView;
	private MyCommentAdapter mAdapter;
	private List<JsonComment> mData;
	private LinearLayout mLayoutEmpty, mLayoutError;

	public final static int PAGE_SIZE = 10;
	private int mPageIndex = 1;//页数

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_my_comment);

		mContext = ActivityMyComment.this;

		setupView();

		setupData();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if(requestCode == Const.CODE_REQUEST_LOGIN && resultCode == Const.CODE_RESPONSE_LOGIN_SUCCESS) {
			setupData();
		} else {
			finish();
		}
	}

	private void setupView() {
		setupToolBar2();//显示标题栏
		mLayoutEmpty = (LinearLayout)findViewById(R.id.tj_empty_view);
		mLayoutError = (LinearLayout)findViewById(R.id.tj_error_view);
		if (mLayoutError != null) {
			mLayoutError.setOnClickListener(this);
		}

		mData = new ArrayList<JsonComment>();
		mAdapter = new MyCommentAdapter(mData, mContext);
		mListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
		if (mListView != null) {
			mListView.setMode(Mode.BOTH);
			mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
				@Override
				public void onPullDownToRefresh(
						PullToRefreshBase<ListView> refreshView) {
					//下拉的时候数据重置
					mMainRequest = ChargeStationAPI.getMyPointComments(
							1, PAGE_SIZE, mResponseReload);
				}
				@Override
				public void onPullUpToRefresh(
						PullToRefreshBase<ListView> refreshView) {
					//上拉的时候添加选项
					mMainRequest = ChargeStationAPI.getMyPointComments(
							mPageIndex + 1, PAGE_SIZE, mResponseMore);
				}
			});
			mListView.setAdapter(mAdapter);
		}

	}

	private void setupData() {
		showMyProgressDialog(true, true, "加载中...");
		//向服务器请求数据
		mMainRequest = ChargeStationAPI.getMyPointComments(mPageIndex, PAGE_SIZE, mResponse);
	}

	private final EHttpResponse mResponse = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMyCommentList result = GsonUtils.toBean(JsonMyCommentList.class, response);
			if(handleResponseStatus(result)) {
				mData.clear();
				mPageIndex = 1;
				if(result.total <= 0) {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
					
				} else {
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					mData.addAll(result.commentItems);
				}
				mAdapter.notifyDataSetChanged();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			dismissMyProgressDialog();
		}
	};

	private final EHttpResponse mResponseReload = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMyCommentList result = GsonUtils.toBean(JsonMyCommentList.class, response);
			if(handleResponseStatus(result)) {
				mData.clear();
				mPageIndex = 1;
				if(result.commentItems != null && result.commentItems.size() > 0){
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					mData.addAll(result.commentItems);
				} else {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
				}
				mAdapter.notifyDataSetChanged();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			mListView.onRefreshComplete();
		}
	};

	private final EHttpResponse mResponseMore = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonMyCommentList result = GsonUtils.toBean(JsonMyCommentList.class, response);
			if(handleResponseStatus(result)) {
				if(result.commentItems != null && result.commentItems.size() > 0){
					UIUtil.hideAll(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.VISIBLE);
					mPageIndex++;
					mData.addAll(result.commentItems);
					mAdapter.notifyDataSetChanged();
				} else {
					UIUtil.showEmpty(mLayoutEmpty, mLayoutError);
					mListView.setVisibility(View.GONE);
					Toast.makeText(mContext, R.string.common_tip_rusult_nomoredata,Toast.LENGTH_SHORT).show();
				}
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			UIUtil.showNetError(mLayoutEmpty, mLayoutError);
			mListView.setVisibility(View.GONE);
		}

		@Override
		public void onEFinish() {
			super.onEFinish();
			mListView.onRefreshComplete();
		}

	};

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, 
			final int position, long arg3) {
		new AlertDialog.Builder(mContext)
		.setTitle("提示")
		.setMessage("确定删除该条评论?")
		.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				System.out.println(mData.get(position).id);
				ChargeStationAPI.deleteComment(mData.get(position).id, mResponseDelete);
			}
		})
		.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		}).show();
		return false;
	}

	private final EHttpResponse mResponseDelete = new EHttpResponse() {

		@Override
		public void onEResponse(String response) {
			JsonBase result = GsonUtils.toBean(JsonBase.class, response);
			if(handleResponseStatus(result)) {
				Toast.makeText(mContext, "删除成功", Toast.LENGTH_SHORT).show();
				setupData();
			}
		}

		@Override
		public void onEErrorResponse(VolleyError error) {
			Toast.makeText(mContext, R.string.common_tip_resultfail,Toast.LENGTH_SHORT).show();
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tj_error_view:
			setupData();
			break;
		default:
			break;
		}
	}

}
